package com.qianfeng.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.bean.JsonResult;
import com.qianfeng.pojo.GoodsCateGory;
import com.qianfeng.service.GoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author wang
 * @date 2021/9/22 18:04
 */
@Controller
@RequestMapping("goods")
public class GoodsCategoryController {

    @Autowired
    private GoodsCategoryService goodsCategoryService;
    @RequestMapping("findByPage")
    @ResponseBody
    public JsonResult findByPage(
            @RequestParam(value = "page",required = false,defaultValue = "1")Integer pageNum,
            @RequestParam(value = "limit",required = false,defaultValue = "3")Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        //查询所有用户
        List<GoodsCateGory> goods = goodsCategoryService.findAll();
        //封装到PageInfo对象中
        PageInfo<GoodsCateGory> pageInfo = new PageInfo<GoodsCateGory>(goods);
        //返回JsonResult对象
        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setCount(pageInfo.getTotal());
        jsonResult.setData(pageInfo.getList());

        return jsonResult;
    }

    @RequestMapping("deleteGoodsById")
    @ResponseBody
    public String deleteGoodsById(Integer cateGoryId){
        goodsCategoryService.deleteGoodsById(cateGoryId);
        return "success";
    }

    @RequestMapping("addGoods")
    public String addGoods(GoodsCateGory goodsCateGory){

        goodsCategoryService.addGoods(goodsCateGory);
        return "redirect:http://localhost:8080/index.html";
    }

    @RequestMapping("updateGoods")
    @ResponseBody
    public String updateGoods(GoodsCateGory goodsCateGory){
        System.out.println(goodsCateGory);
        goodsCategoryService.updateGoods(goodsCateGory);
        return "success";
    }
}
